﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.LabEvaluation
{
    public partial class EvalResult : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYSPGJG");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                BindData();
            }
        }

        //绑定
        protected void BindData()
        {
            LabDropDownList();//评估对象

            //评估标准类别
            KindDDL.DataSource = DictionaryDropdownlist("119");
            KindDDL.DataBind();

            //主题名
            if (KindDDL.SelectedValue != "")
            {
                int code;
                if (int.TryParse(KindDDL.SelectedValue, out code))
                {
                    TitleDropDownList(code);
                }
            }
            //gridview
            if (TitleDDL.SelectedValue != "")
            {
                
                int titleID;
                if (int.TryParse(TitleDDL.SelectedValue, out titleID))
                {
                    DateDropDownList(titleID, int.Parse(LabDDL.SelectedValue));
                    if (DateDDL.SelectedValue != "")
                    {
                        int EvTimeID;
                        if (int.TryParse(DateDDL.SelectedValue, out EvTimeID))
                        {
                            EvalTitleGV_DataBind(titleID, LabDDL.SelectedValue,EvTimeID);
                        }
                    }
                }
            }
            if (KindDDL.SelectedValue == "" || DateDDL.SelectedValue == "" || LabDDL.SelectedValue == "" || TitleDDL.SelectedValue == "")
            {
                mondifybtn.Attributes.Add("disabled", "disabled");
                DeleteBtn.Enabled = false;
            }

        }

        //DropDownList索引改变  标准类型
        protected void KindDDL_OnSelectIndexChange(object sender, EventArgs e)
        {
            int code = int.Parse(KindDDL.SelectedValue);
            TitleDropDownList(code);//绑定主题
            //绑定Gridview
            int ID = 0;
            int Time = 0;
            if (TitleDDL.SelectedValue != "")
            {
                int.TryParse(TitleDDL.SelectedValue, out ID);
            }
            if (LabDDL.SelectedValue != "")
            {
                DateDropDownList(ID, int.Parse(LabDDL.SelectedValue));
                if (DateDDL.SelectedValue != "")
                {
                    int.TryParse(DateDDL.SelectedValue, out Time);
                }
            }
            EvalTitleGV_DataBind(ID, LabDDL.SelectedValue,Time);
            if (DateDDL.SelectedValue == "" || LabDDL.SelectedValue == "" || TitleDDL.SelectedValue == "")
            {
                mondifybtn.Attributes.Add("disabled", "disabled");
                DeleteBtn.Enabled = false;
            }
            else
            {
                mondifybtn.Attributes.Remove("disabled");
                DeleteBtn.Enabled = true;
            }
        }

        //DropDownList索引改变  主题
        protected void TitleDDL_OnSelectIndexChange(object sender, EventArgs e)
        {
            int ID = 0;
            int Time = 0;
            if (TitleDDL.SelectedValue != "")
            {
                int.TryParse(TitleDDL.SelectedValue, out ID);
            }
            if (LabDDL.SelectedValue != "")
            {
                DateDropDownList(ID, int.Parse(LabDDL.SelectedValue));
                if (DateDDL.SelectedValue != "")
                {
                    int.TryParse(DateDDL.SelectedValue, out Time);
                }
            }
            EvalTitleGV_DataBind(ID, LabDDL.SelectedValue, Time);
            if ( DateDDL.SelectedValue == "" || LabDDL.SelectedValue == "" )
            {
                mondifybtn.Attributes.Add("disabled", "disabled");
                DeleteBtn.Enabled = false;
            }
            else
            {
                mondifybtn.Attributes.Remove("disabled");
                DeleteBtn.Enabled = true;
            }
        }

        //DropDownList索引改变  实验室
        protected void LabDDL_OnSelectIndexChange(object sender, EventArgs e)
        {
            int ID = 0;
            int Time = 0;
            if (TitleDDL.SelectedValue != "")
            {
                int.TryParse(TitleDDL.SelectedValue, out ID);
            }
            if (LabDDL.SelectedValue != "")
            {
                DateDropDownList(ID, int.Parse(LabDDL.SelectedValue));
                if (DateDDL.SelectedValue != "")
                {
                    int.TryParse(DateDDL.SelectedValue, out Time);
                }
            }
            EvalTitleGV_DataBind(ID, LabDDL.SelectedValue, Time);
            if (KindDDL.SelectedValue == "" || DateDDL.SelectedValue == "" || TitleDDL.SelectedValue == "")
            {
                mondifybtn.Attributes.Add("disabled", "disabled");
                DeleteBtn.Enabled = false;
            }
            else
            {
                mondifybtn.Attributes.Remove("disabled");
                DeleteBtn.Enabled = true;
            }
        }

        //DropDownList索引改变  日期
        protected void DateDDL_OnSelectIndexChange(object sender, EventArgs e)
        {
            EvalTitleGV_DataBind(int.Parse(TitleDDL.SelectedValue), LabDDL.SelectedValue, int.Parse(DateDDL.SelectedValue));
        }

        //绑定GridView
        protected void EvalTitleGV_DataBind(int TitleID,string strLabID,int EvtimeID)
        {
            DataSet ds = new DataSet();
            ds = GetStandardByTitleID(TitleID, strLabID, EvtimeID);

            EvalResultGridView.DataSource = HandleDS(ds);
            EvalResultGridView.DataBind();
        }
        
        //处理ds
        protected DataSet HandleDS(DataSet ds)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
            {
                ds.Tables[0].Rows[i]["St_Standard"] = ds.Tables[0].Rows[i]["St_Standard"].ToString().Replace("\r\n", "<br/>");
                ds.Tables[0].Rows[i]["St_Method"] = ds.Tables[0].Rows[i]["St_Method"].ToString().Replace("\r\n", "<br/>");
                ds.Tables[0].Rows[i]["Comment"] = ds.Tables[0].Rows[i]["Comment"].ToString().Replace("\r\n", "<br/>");
                ds.Tables[0].Rows[i]["Remark"] = ds.Tables[0].Rows[i]["Remark"].ToString().Replace("\r\n", "<br/>");
            }
            return ds;
        }

        //绑定评估日期
        protected void DateDropDownList(int ID, int LabID)
        {
            ExtendBLL.Standard sta = new ExtendBLL.Standard();
            DataSet ds = new DataSet();

            ds = sta.GetDateList(ID, LabID);
            DateDDL.DataSource = ds;
            DateDDL.DataBind();
        }

        //绑定评估实验室
        protected void LabDropDownList()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> labModel = new List<LabMS.Model.Lab>();

            labModel = lab.GetModelList("Lab_ParentID is null order by [ID] DESC");
            LabDDL.DataSource = labModel;
            LabDDL.DataBind();
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass='" + code + "'";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        //绑定主题
        protected void TitleDropDownList(int code)
        {
            LabMS.BLL.EvaluationTitle evalTitle = new LabMS.BLL.EvaluationTitle();
            List<LabMS.Model.EvaluationTitle> evalTitleModel = new List<LabMS.Model.EvaluationTitle>();

            evalTitleModel = evalTitle.GetModelList("Title_Category=" + code);
            TitleDDL.DataSource = evalTitleModel;
            TitleDDL.DataBind();
        }

        //根据主题ID获取评估标准信息
        protected DataSet GetStandardByTitleID(int TitleID,string strLabID,int EvTimeID)
        {
            ExtendBLL.Standard stan = new ExtendBLL.Standard();
            DataSet ds = new DataSet();
            try
            {
                ds = stan.GetSelfInfoByTitleID(TitleID, strLabID,EvTimeID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        protected void Delete_OnClick(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ExtendBLL.Standard stan = new ExtendBLL.Standard();
            ds = stan.GetSelfInfoByTitleID(int.Parse(TitleDDL.SelectedValue), LabDDL.SelectedValue, int.Parse(DateDDL.SelectedValue));
            string strWhere="1=0";

            for(int i=0;i<ds.Tables[0].Rows.Count;i++)
            {
                strWhere+=" or EID="+int.Parse(ds.Tables[0].Rows[i]["EID"].ToString());
            }
            try
            {
                stan.DeleteEva(strWhere);
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除成功！')</script>");
                DateDropDownList(int.Parse(TitleDDL.SelectedValue), int.Parse(LabDDL.SelectedValue));
                if (DateDDL.SelectedValue != "")
                {
                    EvalTitleGV_DataBind(int.Parse(TitleDDL.SelectedValue),LabDDL.SelectedValue, int.Parse(DateDDL.SelectedValue));
                }
                else
                {
                    EvalResultGridView.DataSource = "";
                    EvalResultGridView.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
